﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcEngine.Core.Services;
using System.Web.Mvc;
using MvcEngine.Mvc.Extensions;
using MvcEngine.Core;
using MvcEngine.Core.Registry;
using MvcEngine.Mvc.Filters;
using MvcEngine.Mvc;
using MvcEngine.Gallery.Models;
using MvcEngine.Gallery.ModelBinders;
using MvcEngine.Gallery.Components;
using MvcEngine.Gallery.StarterSite.Controllers;
using MvcEngine.Mvc.Services;

namespace MvcEngine.Gallery.Services
{
    public class GalleryImageControlService : ControlServiceBase
    {
        public override void RegisterBackofficeRoutes(AreaRegistrationContext context)
        {
            
            context.MapMvcEngineRoute(
                "BackOffice_Gallery",
                "BackOffice/Gallery/{galleryName}",
                new { controller = "GalleryImage", action = "List" },
                new string[] { "MvcEngine.Gallery.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_GalleryImagePage",
                "BackOffice/Gallery/{galleryName}/Images/Page{pageNumber}",
                new { controller = "GalleryImage", action = "List", pageNumber = "0" },
                new string[] { "MvcEngine.Gallery.BackOffice.Controllers" }
            );

            //context.MapMvcEngineRoute(
            //    "BackOffice_GalleryImagePaging",
            //    "BackOffice/gallery/{galleryName}/category/{categoryName}/Page{pageNumber}",
            //    new { controller = "GalleryImage", action = "List", pageNumber = "0" },
            //    new string[] { "MvcEngine.Gallery.BackOffice.Controllers" }
            //);

            context.MapMvcEngineRoute(
                "BackOffice_GalleryNewImage",
                "BackOffice/Gallery/{galleryName}/Image/New",
                new { controller = "GalleryImage", action = "Edit", imageId = "" },
                new string[] { "MvcEngine.Gallery.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_GalleryEditImage",
                "BackOffice/Gallery/{galleryName}/Image/Edit/{id}",
                new { controller = "GalleryImage", action = "Edit" },
                new string[] { "MvcEngine.Gallery.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_GalleryDeleteImage",
                "BackOffice/Gallery/{galleryName}/Image/Delete/{id}",
                new { controller = "GalleryImage", action = "Delete" },
                new string[] { "MvcEngine.Gallery.BackOffice.Controllers" }
            );

        }

        public override void RegisterStarterSiteRoutes(AreaRegistrationContext context)
        {
            context.MapImageRoute(
                "StarterSite_GalleryImageFile",
                "{pageName}/Gallery/{fileName}",
                new GalleryImageFileResolver(),
                new string[] { "MvcEngine.Gallery.StarterSite.Controllers" }
            );

            context.MapModuleRoute(
                "GalleryCategoryFilterPage",
                "{pageName}/gallery/category/{categoryName}/Page{pageNumber}",
                new { actionName = "CategoryFilter" },
                new string[] { "MvcEngine.Gallery.StarterSite.Controllers" }
            );

            context.MapModuleRoute(
                "GalleryCategoryFilter",
                "{pageName}/gallery/category/{categoryName}",
                new { actionName = "CategoryFilter" },
                new string[] { "MvcEngine.Gallery.StarterSite.Controllers" }
            );

            //context.MapMvcEngineRoute(
            //    "StarterSite_GalleryCategoryList",
            //    "{pageName}/gallery/{galleryName}/category/{categoryName}",
            //    new { controller = "GalleryImage", action = "List" },
            //    new string[] { "MvcEngine.Gallery.StarterSite.Controllers" }
            //);

            //context.MapMvcEngineRoute(
            //    "BackOffice_GalleryImagePaging",
            //    "{pageName}/gallery/{galleryName}/category/{categoryName}/page{pageNumber}",
            //    new { controller = "GalleryImage", action = "List", pageNumber = "0" },
            //    new string[] { "MvcEngine.Gallery.StarterSite.Controllers" }
            //);
        }

        public override void RegisterActionFilters(IActionFilterRegistry registry)
        {
            //--------------------------------------------------------------------------------------
            //------------------------------Common filters------------------------------------------
            //--------------------------------------------------------------------------------------
            ControllerCriteria backOfficeCommonCriteria = new ControllerCriteria();
            backOfficeCommonCriteria.AddController(typeof(MvcEngine.Gallery.BackOffice.Controllers.GalleryImageController));
            registry.Add(new[] { backOfficeCommonCriteria }, typeof(MvcEngine.Gallery.BackOffice.Filters.GalleryActionFilter));

            ControllerCriteria starterSiteCommonCriteria = new ControllerCriteria();
            starterSiteCommonCriteria.AddController(typeof(MvcEngine.Gallery.StarterSite.Controllers.GalleryImageController));
            registry.Add(new[] { starterSiteCommonCriteria }, typeof(MvcEngine.Gallery.StarterSite.Filters.GalleryActionFilter));

            ////--------------------------------------------------------------------------------------
            ////------------------------------End of common filters-----------------------------------
            ////--------------------------------------------------------------------------------------


            ControllerCriteria moduleCriteria = new ControllerCriteria();
            moduleCriteria.AddController(typeof(MvcEngine.Gallery.BackOffice.Controllers.GalleryImageController));
            registry.Add(new[] { moduleCriteria }, new ModuleActionFilter());


            ////--------------------------------------------------------------------------------------
            ////---------------------------------Permissions------------------------------------------
            ////--------------------------------------------------------------------------------------
            //ControllerCriteria viewCriteria = new ControllerCriteria();
            //viewCriteria.AddController(typeof(MvcEngine.Blog.StarterSite.Controllers.BlogCategoryController));

            //ControllerCriteria editCriteria = new ControllerCriteria();
            //editCriteria.AddController(typeof(MvcEngine.Blog.BackOffice.Controllers.BlogCategoryController));
            ////--------------------------------------------------------------------------------------
            ////---------------------------------End of permissions----------------------------------
            ////--------------------------------------------------------------------------------------

            //registry.Add(new[] { editCriteria }, new RolePermissionAttribute(Constants.Roles.Administrators));
            //registry.Add(new[] { viewCriteria }, new ModulePermissionAttribute(PermissionTypes.View));
        }

        public override void RegisterModelBinders(ModelBinderDictionary binders)
        {
            binders[typeof(GalleryImage)] = new GalleryImageModelBinder();
        }

        public override IEnumerable<string> GetStyleSheets()
        {
            return new List<string>() { "MvcEngine.Gallery.StarterSite.css" };
        }

        public override Type GetControllerType()
        {
            return typeof(GalleryImageController);
        }

        public override string GetDefaultAction()
        {
            return "CategoryFilter";
        }
    }
}
